package cn.sdjzu.exercise;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

/**
 * 2013-8-16
 * 
 * @author {YueJinbiao}
 */
public class BasedActivity extends Activity {
	public static final String TAG = "Exercise_test";
	public static final String MODE_ADD_MUSCLE = "add_muscle";
	public static final String MODE_ADD_FORCE = ":add_force";
	public static final String MODE_BODYBUILDING = "bodybuilding";
	public static final String MODE_REDUCE_FAT = "reduce_fat";
	public static final String MODE_CUSTOMIZE = "customize";
	public static final String mode_ConstantSpeed_active = "mode_ConstantSpeed_active";
	public static final String mode_ConstantSpeed_passive = "mode_ConstantSpeed_passive";
	public static final String mode_ConstantForce_active = "mode_ConstantForce_active";
	public static final String mode_ConstantForce_passive = "mode_ConstantForce_passive";
	// 缩放比例
	public static float scale = 0;
	public static float scale1 = 0;
	public static float scale2 = 0;
	public static float scale3 = 0;
	public double flat_tureSize = 0;
	public static int TureEditH = 0;
	// 标准UI设计宽度（px）
	private static final float UI_DESIGN_WIDTH = 1024;
	private static final double flat_RelativeSize = 9.5;
	public static final int  EditH=40;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	Toast TransToast;
	Toast DBNoDataToast;
	ReadWriteFile TXT;
	@Override
	public void setContentView(int layoutResID) {
		View view = View.inflate(this, layoutResID, null);
		this.setContentView(view);
	}

	@Override
	public void setContentView(View view) {
		if (scale == 0) {
			initScreenScale();
		}
		if (scale != 1) {
			RelayoutTool.relayoutViewHierarchy(view, scale);
		}
		super.setContentView(view);
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		if (scale == 0) {
			initScreenScale();
		}
		RelayoutTool.relayoutViewHierarchy(view, scale);
		RelayoutTool.scaleLayoutParams(params, scale);
		super.setContentView(view, params);
	}

	/**
	 * 按屏幕宽度与设计宽度初始化缩放比例
	 */
	public void initScreenScale() {
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		float width = displayMetrics.widthPixels; // 屏幕宽度（像素）
		float height = displayMetrics.heightPixels; // 屏幕高度（像素）
		scale1 = width / UI_DESIGN_WIDTH;
		scale2 = 1 / (displayMetrics.density);
		
		float densityDpi = displayMetrics.densityDpi;
		flat_tureSize = (float) (Math.sqrt(width * width + height * height) / densityDpi);
		double scale4 = flat_tureSize / flat_RelativeSize;
		scale3 = (float) scale4;
		if (scale1 > 1 && scale2 <= 1) {
			scale = scale3;
		} else {
			scale = scale1*scale2*scale3;
		}
		TureEditH=(int) (EditH*scale);
	}
	@Override
	public void onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction("ExitApp");
		this.registerReceiver(broadcastReceiver, filter);
	}
	protected void onDestroy() {
		super.onDestroy();
	}
	
	public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			finish();
		}
	};
	public void close() {
		onDestroy();
		Intent intent = new Intent();
		intent.setAction("ExitApp");
		this.sendBroadcast(intent);
		super.finish();
	}
}